u = "/geo_current_confirm"
var chart_mid = echarts.init(document.getElementById('mid'), 'macarons', {renderer: 'canvas'});
var chart_left1 = echarts.init(document.getElementById('left1'), 'macarons', {renderer: 'canvas'});
var chart_left2 = echarts.init(document.getElementById('left2'), 'macarons', {renderer: 'canvas'});
var chart_left3 = echarts.init(document.getElementById('left3'), 'macarons', {renderer: 'canvas'});
var chart_right2 = echarts.init(document.getElementById('right2'), 'macarons', {renderer: 'canvas'});
var chart_right1 = echarts.init(document.getElementById('right1'), 'macarons', {renderer: 'canvas'});
var chart_mid_left = echarts.init(document.getElementById('right_mid_left'), 'macarons', {renderer: 'canvas'});
var chart_mid_right = echarts.init(document.getElementById('right_mid_right'), 'macarons', {renderer: 'canvas'});

//图表设置，有些js设置ajax传不过来，只能在这里补上
var geo_option = {
		"tooltip": {
		"triggerOn": "mousemove|click",
		"formatter": function (params) {        return params.name + ' : ' + params.value[2];    },
	},
};
var liquid_option = {
	"label": {
		"formatter": function (param) {                        return (Math.floor(param.value * 10000) / 100) + '%';                    }
	},
};
//预先绘制地图，避免看着太空旷
var geo_background_option={
	"geo": {
		"map": "china",
		"layoutSize": "50%",
		"itemStyle": {
			"color": "#5F7DE1",
			"borderColor": "#FFFFFF"
		},
	}
};
chart_mid.setOption(geo_background_option);


/*按钮时切换url，并立即执行ajax一次*/
function change_geo_to_danger_areas(){
	u = "/mapChart"
	$.ajax({
		type: "GET",
		url: u,
		dataType: "json",
		success: function (result) {
			chart_mid.setOption(result);
			chart_mid.setOption(geo_option);
		}
	});
}
function change_geo_to_confirm(){
	u = "/geo_current_confirm"
	$.ajax({
		type: "GET",
		url: u,
		dataType: "json",
		success: function (result) {
			chart_mid.setOption(result);
			chart_mid.setOption(geo_option);
		}
	});
}
function change_geo_to_suspected(){
	u = "/geo_current_suspected"
	$.ajax({
		type: "GET",
		url: u,
		dataType: "json",
		success: function (result) {
			chart_mid.setOption(result);
			chart_mid.setOption(geo_option);
		}
	});
}

//按时同步各个图表
$(
	function () {
		fetchData(chart_mid,  chart_left1,  chart_left2, chart_left3, chart_right2, chart_right1,
		 chart_mid_left, chart_mid_right);
		/*设置与后台同步数据的间隔*/
		setInterval(fetchData, 1000 * 60 * 30);
	}
);
//更新后台的covchart对象
function updateDate(){
   $.ajax({
		type: "GET",
		url: "/updateCovData",
		dataType: "json",
		success: function (result) {
            console.log(result);
		}
	});
}
//所有需要更新图表的ajax都在这个函数里
function fetchData(){
    updateDate();
	$.ajax({
		type: "GET",
		url: u,
		dataType: "json",
		success: function (result) {
			/*用返回值设置前面定义的echarts对象，使用setOption这个方法，设置完，就能渲染出图表*/
			chart_mid.setOption(result);
			chart_mid.setOption(geo_option);
		}
	});

	$.ajax({
		type: "GET",
		url: "/pieChart",
		dataType: "json",
		success: function (result) {
			chart_left2.setOption(result);
		}
	});
	$.ajax({
		type: "GET",
		url: "/lineChart",
		dataType: "json",
		success: function (result) {
			chart_left1.setOption(result);
		}
	});

	$.ajax({
		type: "GET",
		url: "/lineChartCurrentConfirm",
		dataType: "json",
		success: function (result) {
			chart_right2.setOption(result);
		}
	});

	$.ajax({
		type: "GET",
		url: "/BarChartCurrentConfirm",
		dataType: "json",
		success: function (result) {
			chart_right1.setOption(result);
		}
	});
	$.ajax({
		type: "GET",
		url: "/cureRates",
		dataType: "json",
		success: function (result) {
		 chart_mid_left.setOption(result);
		 chart_mid_left.setOption(liquid_option);
		}
	});
	$.ajax({
		type: "GET",
		url: "/deathRates",
		dataType: "json",
		success: function (result) {
		 chart_mid_right.setOption(result);
		 chart_mid_right.setOption(liquid_option);
		}
	});
	$.ajax({
		type: "GET",
		url: "/line_serious_chart",
		dataType: "json",
		success: function (result) {
		 chart_left3.setOption(result);
		}
	});
	$.ajax({
		type: "GET",
		url: "/CovNums",
		dataType: "json",
		success: function (result) {
			var res_confirm = result.currentConfirmedCount;
			var res_suspected = result.suspectedCount;
			var res_serious = result.seriousCount;
			var res_dangers = result.dangerAreas;

			$("#currentConfirm").html(res_confirm);
			$("#suspectedCount").html(res_suspected);
			$("#seriousCount").html(res_serious);
			$("#dangerAreas").html(res_dangers);
		}
	});
}
